use bytes::BytesMut;

use crate::{extract_fixed_data, RespDecode, RespEncode, RespError};

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct RespNull;

// _\r\n
impl RespEncode for RespNull {
    fn encode(self) -> Vec<u8> {
        b"_\r\n".to_vec()
    }
}

impl RespDecode for RespNull {
    fn decode(buf: &mut BytesMut) -> Result<Self, RespError> {
        match extract_fixed_data(buf, "_\r\n", "Nulls") {
            Ok(_) => Ok(RespNull),
            Err(e) => Err(e),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::RespFrame;

    #[test]
    fn null_encode_test() {
        let frame: RespFrame = RespNull.into();
        assert_eq!(frame.encode(), b"_\r\n");
    }
}
